﻿using System;
using System.Linq;
using PersonalAccoutingProgram.Data;
using System.Text;
using PersonalAccoutingProgram.Logic;

class IncomesReferenceMaker : ReferenceMaker 
{
    public static decimal MakeAverageReferenceDay(
        DateTime date, string username, string category)
    {
       
        decimal incomesSum = 0;       

        if (category == String.Empty)
        {
            var allIncomesForDate = 
                IncomeViewDataClassesDataContext.GetIncomesForDay(
                date, username);
           incomesSum = CalculateAverageAmount(allIncomesForDate);
        }
        else
        {
            var allIncomesForDate =
                IncomeViewDataClassesDataContext.GetIncomesForDayByCategory(
                date, username, category);
            incomesSum = CalculateAverageAmount(allIncomesForDate);
        }

        return incomesSum;
    }  

    public static decimal MakeTotalReferenceDay(
        DateTime date, string username, string category)
    {
        decimal incomesSum = 0;
        
        if (category == String.Empty)
        {
            var allIncomesForDate =
                IncomeViewDataClassesDataContext.GetIncomesForDay(
                date, username);

            incomesSum = CalculateAverageAmount(allIncomesForDate);
        }
        else
        {
            var allIncomesForDate =
             IncomeViewDataClassesDataContext.GetIncomesForDayByCategory(
                date, username, category);

            incomesSum = CalculateAverageAmount(allIncomesForDate);
        }

        return incomesSum;
    }

    public static decimal MakeTotalReferenceMonth(
        string date, string username, string category)
    {
        decimal incomesSum = 0;
        DateTime dateForCheck = DateTime.Parse(date);
        
        if (category == String.Empty)
        {
            var allIncomesForUser =
                IncomeViewDataClassesDataContext.GetIncomesForMonth(
                username, dateForCheck);

            incomesSum = CalculateAverageAmount(allIncomesForUser);
        }
        else
        {
           var allIncomesForUser =
               IncomeViewDataClassesDataContext.GetUserIncomesForMonthByorigin(
               username, category, dateForCheck);

           incomesSum = CalculateAverageAmount(allIncomesForUser);
        }

        return incomesSum;
    }   

    public static decimal MakeTotalReferenceYear(
        string date, string username, string category)
    {

        decimal incomesSum = 0;
        DateTime dateForCheck = DateTime.Parse(date);

        if (category == String.Empty)
        {
            var allIncomesForUser =
            IncomeViewDataClassesDataContext.GetIncomesForYear(
                username, dateForCheck);
            incomesSum = CalculateSum(allIncomesForUser);
        }
        else
        {
            var allIncomesForUser =
            IncomeViewDataClassesDataContext.GetIncomesForYearByCategory(
                username, category, dateForCheck);
            incomesSum = CalculateSum(allIncomesForUser);
        }

        return incomesSum;
    }

    public static string GetAllAmountsForCurrentUser(string username, string category)
    {
        string incomesStr = String.Empty;
        if (category == String.Empty)
        {
            var allIncomesForUser =
            IncomeViewDataClassesDataContext.GetAllUserIncomes(username);
            incomesStr = GetAllExpensesString(allIncomesForUser);
            return incomesStr;
        }
        else
        {
            var allIncomesForUser =
            IncomeViewDataClassesDataContext.GetAllUserIncomesByCategory(
                username, category);
            incomesStr = GetAllExpensesString(allIncomesForUser);
            return incomesStr;
        }
    }

    private static string GetAllExpensesString(IQueryable<IncomesView> allExpensesForUser)
    {
        if (allExpensesForUser.Count(exp => exp.IncomesAmount != 0.00M) > 0)
        {
            string amountStr = MakeAllExpensesString(allExpensesForUser);
            return amountStr.ToString();
        }
        else
        {
            return String.Empty;
        }
    }

    private static string MakeAllExpensesString(IQueryable<IncomesView> allExpensesForUser)
    {
        StringBuilder amountStr = new StringBuilder();
        foreach (var currentExpenses in allExpensesForUser)
        {
            string date = currentExpenses.IncomesDate.ToString(DateFormat.DATE_TIME_FORMAT);
            string categoryStr = currentExpenses.OriginName;
            decimal amount = currentExpenses.IncomesAmount;
            amountStr.AppendFormat("{0} {1,2} {2:C}", date, categoryStr, amount);
            amountStr.AppendLine();
        }
        return amountStr.ToString();
    }
}